<?php

class AuthItem extends MyModel
{
    // {{{ Members
    /**
     * The followings are the available columns in table 'AuthItem':
     * @var string $name
     * @var integer $type
     * @var string $description
     * @var string $bizrule
     * @var string $data
     */

    const TYPE_ROLE         = 2;
    const TYPE_TASK         = 1;
    const TYPE_OPERATION    = 0;
    
    var $nameFormat = "{1}";
    var $nameFields = "name";

    public $itemParents;
    public $createCRUDOperations = 0;
    public $modelName;
    // }}} 
    // {{{ model
    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    } // }}} 
    // {{{ tableName
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'AuthItem';
    } // }}} 
    // {{{ rules
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array('name','length','max'=>64),
            array('name, type', 'required'),
            array('type', 'numerical', 'integerOnly'=>true),
            array('itemParents,createCRUDOperations,modelName','safe'),
        );
    } // }}} 
    // {{{ relations
    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'authItemChildren' => array(self::HAS_MANY, Yii::app()->authManager->itemChildTable, 'parent'),
            'authAssignments' => array(self::HAS_MANY, Yii::app()->authManager->assignmentTable, 'itemname'),
        );
    } // }}} 
    // {{{ attributeLabels
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'name'          => _('Name'),
            'type'          => _('Type'),
            'description'   => _('Description'),
            'bizrule'       => _('Business Rule'),
            'data'          => _('Data'),
            'createCRUDOperations' => _('Create CRUD Operations'),
            'modelName'            => _('Model Name (singular)'),
        );
    } // }}} 
    // {{{ getTypeOptions
    public function getTypeOptions()
    {
        return array(
            self::TYPE_ROLE         => Yii::t('site','Role'),
            self::TYPE_TASK         => Yii::t('site','Task'),
            self::TYPE_OPERATION    => Yii::t('site','Operation'),
        );
    } // }}} 
    public function getSelectOptions()
    {
        $items = $this->findAll(array('order'=>'type DESC, name'));
        $result = array();
        foreach($items as $item)
            $result[$this->typeOptions[$item->type]][$item->name] = $item->name;
        return $result;
    }
}
